﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DriveBeamEditor.Controls.General;
using JBeamLibrary;
using JBeamLibrary.VehicleParts;

namespace DriveBeamEditor.Controls.BeamNG.Buttons
{
    public partial class PartSelectionButton : XButton
    {
        #region Fields

        private Vehicle _vehicle;

        public event EventHandler VehicleChanged;
        public event EventHandler<VehiclePart> VehiclePartSelected;

        #endregion

        #region Properties

        public Vehicle Vehicle
        {
            get { return _vehicle; }
            set
            {
                if (_vehicle == value) return;
                _vehicle = value;
                OnVehicleChanged();
            }
        }

        public VehiclePart SelectedVehiclePart
        {
            get { return pcVehicleHierarchy.SelectedVehiclePart; }
            set { pcVehicleHierarchy.SelectedVehiclePart = value; }
        }

        #endregion

        #region CTORs

        public PartSelectionButton()
        {
            InitializeComponent();
        }

        #endregion

        #region Events

        protected override void OnMouseClick(MouseEventArgs e)
        {
            base.OnMouseClick(e);

            if (!pddVehicleHierarchy.Visible)
                pddVehicleHierarchy.Show(this);
        }

        private void pcVehicleHierarchy_VehiclePartSelected(object sender, VehiclePart e)
        {
            if (VehiclePartSelected != null)
                VehiclePartSelected(this, e);
        }

        #endregion

        #region Methods

        protected virtual void BindVehicle()
        {
            pcVehicleHierarchy.Vehicle = Vehicle;
        }

        protected virtual void OnVehicleChanged()
        {
            BindVehicle();

            if (VehicleChanged != null)
                VehicleChanged(this, new EventArgs());
        }

        #endregion
    }
}
